
#ifndef COMMON_QUEUE_H_
#define COMMON_QUEUE_H_

#include <stdlib.h>
#include <deque>

#include "fiber-env/fiber-env.h"
#include "./auto-lock.h"

namespace fasmio { namespace common {

template <typename Type>
class Queue
{
public:
    Queue();
    ~Queue();

public:
    bool Push(Type *data);
    Type* Pop(bool blocking = true, unsigned int timeout = 0);
    void TaskDone(unsigned int count = 1);
    bool Join();
    unsigned int Size() const;

private:
    std::deque<Type*> queue_;
    unsigned int unfinished_;
    mutable std::unique_ptr<fiber_env::IMutex> lock_;
    mutable std::unique_ptr<fiber_env::ICondition> not_empty_;
    mutable std::unique_ptr<fiber_env::ICondition> all_task_done_;
};

}}  // namespace fasmio::common

#include "./queue-aux.h"
#endif  // COMMON_QUEUE_H_

